﻿using PPSMS_Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPSMS.Commons
{
    public static class ReadOPCToList
    {
        /// <summary>
        /// 从OPC读取所有逆变器状态
        /// </summary>
        /// <returns></returns>
        public static List<InverterStatus> GetAllInverterStatus(List<InverterDevices> inverterDevices, string timestamp)
        {
            var inverterStatusList = new List<InverterStatus>();
            int i = 0;
            foreach (var inverter in inverterDevices)
            {
                i++;
                var inverterStatus = ReadInverterStatusFromOPC(i, inverter, timestamp);
                inverterStatusList.Add(inverterStatus);
            }
            return inverterStatusList;
        }
        /// <summary>
        /// 从OPC读取逆变器状态
        /// </summary>
        /// <param name="i"></param>
        /// <param name="inverter"></param>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static InverterStatus ReadInverterStatusFromOPC(int i, InverterDevices inverter, string timestamp = "")
        {
            var inverterStatus = new InverterStatus()
            {
                InverterID = inverter.InverterID,
                Status = "正常",
                Timestamp = timestamp,
                ArrayVoltage = Extension.ReadOPCValue($"Channel1.逆变器{i}.arrayVoltage"),
                ArrayCurrent = Extension.ReadOPCValue($"Channel1.逆变器{i}.arrayCurrent"),
                GridVoltage = Extension.ReadOPCValue($"Channel1.逆变器{i}.gridConnectedVoltage"),
                CurrentA = Extension.ReadOPCValue($"Channel1.逆变器{i}.la"),
                CurrentB = Extension.ReadOPCValue($"Channel1.逆变器{i}.lb"),
                CurrentC = Extension.ReadOPCValue($"Channel1.逆变器{i}.lc"),
                Frequency = Extension.ReadOPCValue($"Channel1.逆变器{i}.frequency"),
                InternalTemperature = Extension.ReadOPCValue($"Channel1.逆变器{i}.internalTemperature"),
                CurrentPower = Extension.ReadOPCValue($"Channel1.逆变器{i}.currentPowerGeneration"),
                AveragePower = Extension.ReadOPCValue($"Channel1.逆变器{i}.averagePowerGeneration"),
            };
            RecordInverterStatusLog(inverterStatus);
            return inverterStatus;
        }


        /// <summary>
        /// 从OPC读取所有逆变器统计数据
        /// </summary>
        /// <param name="inverterDevices"></param>
        /// <returns></returns>
        public static List<InverterStatistics> GetAllInverterStatistics(List<InverterDevices> inverterDevices, string timestamp)
        {
            var inverterStatisticsList = new List<InverterStatistics>();
            int i = 0;

            foreach (var inverter in inverterDevices)
            {
                i++;
                var inverterStatistics = ReadInverterStatisticsFromOPC(i, inverter, timestamp);
                inverterStatisticsList.Add(inverterStatistics);
            }
            return inverterStatisticsList;
        }
        /// <summary>
        /// 从OPC读取逆变器统计数据
        /// </summary>
        /// <param name="i"></param>
        /// <param name="inverter"></param>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static InverterStatistics ReadInverterStatisticsFromOPC(int i, InverterDevices inverter, string timestamp = "")
        {
            var inverterStatistics = new InverterStatistics()
            {
                InverterId = inverter.InverterID,
                Date = timestamp,
                DailyEnergy = Extension.ReadOPCValue($"Channel1.逆变器{i}.dailyElectricityGeneration"),
                CumulativeEnergy = Extension.ReadOPCValue($"Channel1.逆变器{i}.accumulatedPowerGeneration"),
                CO2Reduction = Extension.ReadOPCValue($"Channel1.逆变器{i}.CO2Reduction"),
            };
            return inverterStatistics;
        }

        /// <summary>
        /// 从OPC读取气象信息
        /// </summary>
        /// <returns></returns>
        public static WeatherInfo ReadWeatherInfoFromOPC(string timestamp)
        {
            var weatherInfo = new WeatherInfo()
            {
                Timestamp = timestamp,
                Irradiance = Extension.ReadOPCValue("PLC.设备1.Irradiance", true),
                Humidity = Extension.ReadOPCValue("PLC.设备1.Humi", true),
                Temperature = Extension.ReadOPCValue("PLC.设备1.Temp", true),
                Windspeed = Extension.ReadOPCValue("PLC.设备1.WindSpeed", true),
            };
            return weatherInfo;
        }
        /// <summary>
        /// 记录逆变器状态日志
        /// </summary>
        /// <param name="inverterStatus"></param>
        private static void RecordInverterStatusLog(InverterStatus inverterStatus)
        {
            if (inverterStatus is null) return;
            if (inverterStatus.InternalTemperature > 70 || inverterStatus.InternalTemperature < -20)
            {
                NLogHelper.Error($"逆变器{inverterStatus.InverterID}内部温度异常:{inverterStatus.InternalTemperature}度");
            }
            if (inverterStatus.GridVoltage > 380 || inverterStatus.GridVoltage < 200)
            {
                NLogHelper.Error($"逆变器{inverterStatus.InverterID}电网电压异常:{inverterStatus.GridVoltage}V");
            }
            if (inverterStatus.ArrayVoltage > 150 || inverterStatus.ArrayVoltage < 100)
            {
                NLogHelper.Error($"逆变器{inverterStatus.InverterID}电池组电压异常:{inverterStatus.ArrayVoltage}V");
            }
            if (inverterStatus.ArrayCurrent > 22 || inverterStatus.ArrayCurrent < 18)
            {
                NLogHelper.Error($"逆变器{inverterStatus.InverterID}电池组电流异常:{inverterStatus.ArrayCurrent}A");
            }
            if (inverterStatus.CurrentA > 4 || inverterStatus.CurrentA < 3)
            {
                NLogHelper.Error($"逆变器{inverterStatus.InverterID}A相电流异常:{inverterStatus.CurrentA}A");
            }
        }
    }
}
